<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderNo">
      <el-input v-model="dataForm.orderNo" disabled placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="学员No" prop="studentNo">
      <el-input v-model="dataForm.studentNo" disabled placeholder="学员No"></el-input>
    </el-form-item>
    <el-form-item label="账号" prop="mobile">
      <el-input v-model="dataForm.mobile" disabled placeholder="登录账号"></el-input>
    </el-form-item>
    <!-- <el-form-item label="总金额" prop="totalFee">
      <el-input v-model="dataForm.totalFee" placeholder="订单金额"></el-input>
    </el-form-item> -->
    <el-form-item label="微信支付" prop="wxFee">
      <el-input v-model="dataForm.wxFee" placeholder="订单金额"></el-input>
    </el-form-item>
    <el-form-item label="支付宝支付" prop="zfbFee">
      <el-input v-model="dataForm.zfbFee" placeholder="订单金额"></el-input>
    </el-form-item>
    <el-form-item label="现金支付" prop="cashFee">
      <el-input v-model="dataForm.cashFee" placeholder="订单金额"></el-input>
    </el-form-item>
    <!-- <el-form-item label="支付类型" prop="payType">
      <el-select v-model="dataForm.payType" disabled  placeholder="请选择">
        <el-option
          v-for="item in payTypeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item> -->
    <el-form-item label="订单状态" prop="status">
     <el-select v-model="dataForm.status" disabled  placeholder="请选择">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getNowFormatDate } from '@/utils'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderNo: '',
          studentNo: '',
          unionId: '',
          nickname: '',
          totalFee: '',
          payType: '',
          status: '',
          isDeleted: '',
          gmtCreate: '',
          gmtModified: '',
          mobile: '',
          wxFee: '',
          zfbFee: '',
          cashFee: '',
          
        },
        eduOrderRecordEntityList: [],
        id: '',
        payTypeOptions: [{
            value: 1,
            label: '微信'
          }, {
            value: 2,
            label: '支付宝'
          },
           {
            value: 3,
            label: '现金'
          }
        ],
        statusOptions: [
          {
            value: 0,
            label: '未支付'
          },{
            value: 1,
            label: '已支付'
          }, {
            value: 2,
            label: '退款'
          }
        ],
        dataRule: {
          orderNo: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          studentNo: [
            { required: true, message: '学员no不能为空', trigger: 'blur' }
          ],
          // unionId: [
          //   { required: true, message: '微信用户unionid不能为空', trigger: 'blur' }
          // ],
          // nickname: [
          //   { required: true, message: '会员昵称不能为空', trigger: 'blur' }
          // ],
          totalFee: [
            { required: true, message: '订单金额不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '支付类型不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态（0：未支付 1：已支付 2: 退款）不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/eduorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.id = data.eduOrder.id;
                this.dataForm.orderNo = data.eduOrder.orderNo
                this.dataForm.studentNo = data.eduOrder.studentNo
                this.dataForm.unionId = data.eduOrder.unionId
                this.dataForm.nickname = data.eduOrder.nickname
                this.dataForm.totalFee = data.eduOrder.totalFee
                this.dataForm.payType = data.eduOrder.payType
                this.dataForm.status = data.eduOrder.status
                this.dataForm.isDeleted = data.eduOrder.isDeleted
                this.dataForm.gmtCreate = data.eduOrder.gmtCreate
                this.dataForm.gmtModified = data.eduOrder.gmtModified
                this.dataForm.mobile = data.eduOrder.mobile
                this.gmtCreate = data.eduOrder.gmtCreate
                if(data.eduOrder.eduOrderRecordEntityList.length) {
                  let list = data.eduOrder.eduOrderRecordEntityList;
                  // this.eduOrderRecordEntityList = list;
                  list.forEach( item =>{
                    if(item.payType == 1) {
                      this.dataForm.wxFee = item.fee;
                    }else if(item.payType == 2) {
                      this.dataForm.zfbFee = item.fee;
                    }else if(item.payType == 3) {
                      this.dataForm.cashFee = item.fee;
                    }

                  })
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        
        if(this.dataForm.wxFee) {
          let obj = {};
          obj.payType = 1
          obj.fee = this.dataForm.wxFee;
          obj.orderNo = this.dataForm.orderNo;
          this.eduOrderRecordEntityList.push(obj);
        }
        if(this.dataForm.zfbFee) {
          let obj = {};
          obj.payType = 2
          obj.fee = this.dataForm.zfbFee;
          obj.orderNo = this.dataForm.orderNo;
          this.eduOrderRecordEntityList.push(obj);
        }
        if(this.dataForm.cashFee) {
          let obj = {};
          obj.payType = 3
          obj.fee = this.dataForm.cashFee;
          obj.orderNo = this.dataForm.orderNo;
          this.eduOrderRecordEntityList.push(obj);
        }
        let price = Number(this.dataForm.wxFee || 0) + Number(this.dataForm.zfbFee || 0) + Number(this.dataForm.cashFee);
        
        
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/eduorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.id || undefined,
                // 'orderNo': this.dataForm.orderNo,
                // 'studentNo': this.dataForm.studentNo,
                // 'unionId': this.dataForm.unionId,
                // 'nickname': this.dataForm.nickname,
                // 'totalFee': this.dataForm.totalFee,
                // 'payType': this.dataForm.payType,
                // 'status': this.dataForm.status,
                // 'mobile': this.dataForm.mobile,
                'orderRecordEntity': this.eduOrderRecordEntityList,
                'price': price
                // 'isDeleted': this.dataForm.isDeleted,
                // 'gmtCreate': this.dataForm.id? this.gmtCreate : getNowFormatDate(),
                // 'gmtModified': getNowFormatDate()
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
